#!/bin/sh
## random a lock image from given path, generate ~/.i3lock.png   

# 获取当前窗口截图照片 创建新图
#import -window root /tmp/screenshot.png
set -o errexit -o noclobber -o nounset

hue=(-level "0%,100%,0.6")
effect=(-filter Gaussian -resize 20% -define "filter:sigma=1.5" -resize 500.5%)
# default system sans-serif font
font=$(convert -list font | awk "{ a[NR] = \$2 } /family: $(fc-match sans -f "%{family}\n")/ { print a[NR-1]; exit }")
image=$(mktemp --suffix=.png)
shot=(import -silent -window root)
desktop=""
i3lock_cmd=(i3lock -k -i "$image")
shot_custom=false

# l10n support
#text="Type password to unlock"
#text="键入解锁的密码($(whoami))"
#text="解锁密码($(whoami)):"
#text="解锁密码\n($(whoami)):"
text="$(whoami)\n解锁密码"
#text="解锁"

command -- "${shot[@]}" "$image"

value="60" #brightness value to compare to

color=$(convert "$image" -gravity center -crop 100x100+0+0 +repage -colorspace hsb \
    -resize 1x1 txt:- | awk -F '[%$]' 'NR==2{gsub(",",""); printf "%.0f\n", $(NF-1)}');

param=("--insidecolor=ffffff" \
       "--ringcolor=ffffff" \
       "--linecolor=ffffff" \
       "--keyhlcolor=000000" \
       "--ringvercolor=000000" \
       "--separatorcolor=222222" \
       "--insidevercolor=000000" \
       "--ringwrongcolor=000000" \
       "--insidewrongcolor=000000" \
       "--verifcolor=ffffff" \
       "--wrongcolor=ff0000" \
       "--timecolor=ff0000" \
       "--datecolor=D3D3D3" \
       "--layoutcolor=000000")

if [[ $color -gt $value ]]; then #white background image and black text
    bw="black"
    #icon="/usr/share/i3lock-fancy/icons/lockdark.png"
    icon="${HOME}/.dwm/scripts/res/icons/circlelockclear.png"
    param=("--inside-color=D8BFD8" \
           "--ring-color=D8BFD8" \
           "--line-color=D8BFD8" \
           "--keyhl-color=6495ED" \
           "--separator-color=D8BFD8" \
           "--insidever-color=D8BFD8" \
           "--ringwrong-color=DC143C" \
           "--insidewrong-color=D8BFD8" \
           "--verif-color=D8BFD8" \
           "--wrong-color=D8BFD8" \
           "--time-color=20B2AA" \
           "--date-color=20B2AA" \
           "--layout-color=D8BFD8")
else #black
    bw="white"
    #icon="/usr/share/i3lock-fancy/icons/lock.png"
    icon="${HOME}/.dwm/scripts/res/icons/circlelockdark.png"
    param=("--inside-color=D8BFD8" \
           "--ring-color=D8BFD8" \
           "--line-color=D8BFD8" \
           "--keyhl-color=6495ED" \
           "--separator-color=D8BFD8" \
           "--insidever-color=D8BFD8" \
           "--ringwrong-color=DC143C" \
           "--insidewrong-color=D8BFD8" \
           "--verif-color=D8BFD8" \
           "--wrong-color=D8BFD8" \
           "--time-color=20B2AA" \
           "--date-color=20B2AA" \
           "--layout-color=D8BFD8")

fi


# 处理图片
#convert /tmp/screenshot.png -resize "1920x1080"^ -gravity center -crop 1920x1080+0+0 +repage "$icon" -gravity center   -blur 0x5  -composite /tmp/screenshotblur.png

convert "$image" "${hue[@]}" "${effect[@]}" -font "$font" -pointsize 26 -fill "$bw" -gravity center \
    -annotate +0+160 "$text" "$icon" -gravity center -composite "$image"

# -gravity 即指定坐标原点，有northwest：左上角，north：上边中间，northeast：右上角，east：右边中间……

# 删除旧图
#rm /tmp/screenshot.png
# 使用当前屏幕模糊化图片进行锁屏
#i3lock  -i /tmp/screenshotblur.png

# 锁屏 添加时间
#i3lock  -k -i  "$image"
i3lock  -k -i  "$image" "${param[@]}" 

#i3lock-fancy 
# 锁屏 添加时间+不用验证
#i3lock  -k --no-verify -i /tmp/screenshotblur.png

#sleep 1
#exit 0
